#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>
MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    this->normalImgPath = normalImg;
    this->pressImgPath = pressImg;
    QPixmap pix;
    bool ret = pix.load(normalImg);
    if (!ret)
    {
        qDebug() << "图片加载失败";
        return;
    }
    this->setFixedSize(pix.width(), pix.height());//设置图片大小
    this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
    this->setIcon(pix);//设置图标
    this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
}
void MyPushButton::zoom1()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");//创建动态对象
    animation->setDuration(200);//设置动画时间间隔
    animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));//起始位置
    animation->setEndValue(QRect(this->x(), this->y()+10, this->width(), this->height()));//结束位置
    animation->setEasingCurve(QEasingCurve::OutBounce);//设置弹跳曲线
    animation->start();//执行动画
}
void MyPushButton::zoom2()
{
    QPropertyAnimation* animation = new QPropertyAnimation(this, "geometry");//创建动态对象
    animation->setDuration(200);//设置动画时间间隔
    animation->setStartValue(QRect(this->x(), this->y()+10, this->width(), this->height()));//起始位置
    animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));//结束位置
    animation->setEasingCurve(QEasingCurve::OutBounce);//设置弹跳曲线
    animation->start();//执行动画
}
/*void MyPushButton::mousePressEvent(QMouseEvent* e)
{
    if(this->pressImgPath!=nullptr)//传入的按下图片不为空，说明需要有按下状态，切换图片
    {
        QPixmap pix;
        bool ret = pix.load(this->pressImgPath);
        if (!ret)
        {
            qDebug() << "图片加载失败";
            return;
        }
        this->setFixedSize(pix.width(), pix.height());//设置图片大小
        this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
        this->setIcon(pix);//设置图标
        this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
    }
    return MyPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent* e)
{
    if(this->pressImgPath!=nullptr)//传入的按下图片不为空，说明需要有按下状态，切换成初始图片
    {
        QPixmap pix;
        bool ret = pix.load(this->normalImgPath);
        if (!ret)
        {
            qDebug() << "图片加载失败";
            return;
        }
        this->setFixedSize(pix.width(), pix.height());//设置图片大小
        this->setStyleSheet("QPushButton{border:Opx;}");//设置不规则图片样式
        this->setIcon(pix);//设置图标
        this->setIconSize(QSize(pix.width(), pix.height()));//设置图标大小
    }
    return MyPushButton::mouseReleaseEvent(e);
}
*/

